package com.harmonk.firstscene.ui.model

import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewModelScope
import com.harmonk.firstscene.FirstSceneApplication
import com.harmonk.firstscene.db.entity.EventNote
import kotlinx.coroutines.launch

class EventNoteViewModel(application: Application) : AndroidViewModel(application) {
    private val trafficNoteRepository = (application as FirstSceneApplication).eventNoteRepository
    
    private val _eventNotes = MutableLiveData<List<EventNote>>()
    val eventNotes: LiveData<List<EventNote>> = _eventNotes
    
    private val _currentEventNote = MutableLiveData<EventNote>()
    val currentEventNote: LiveData<EventNote> = _currentEventNote
    
    fun loadTrafficNotesByEventId(eventId: Long) {
        viewModelScope.launch {
            try {
                trafficNoteRepository.getTrafficNotesByEventId(eventId).collect { eventNoteList: List<EventNote> ->
                    _eventNotes.postValue(eventNoteList)
                }
            } catch (e: Exception) {
                // Handle exception
                _eventNotes.postValue(emptyList())
            }
        }
    }
    
    fun getTrafficNoteById(id: Long) {
        viewModelScope.launch {
            try {
                val trafficNote = trafficNoteRepository.getTrafficNoteById(id)
                trafficNote?.let { t: EventNote ->
                    _currentEventNote.postValue(t)
                }
            } catch (e: Exception) {
                // Handle exception
            }
        }
    }
    
    fun insertTrafficNote(eventNote: EventNote): Long {
        var id: Long = -1
        viewModelScope.launch {
            try {
                id = trafficNoteRepository.insertTrafficNote(eventNote)
            } catch (e: Exception) {
                // Handle exception
            }
        }
        return id
    }
    
    fun updateTrafficNote(eventNote: EventNote) {
        viewModelScope.launch {
            try {
                trafficNoteRepository.updateTrafficNote(eventNote)
            } catch (e: Exception) {
                // Handle exception
            }
        }
    }
    
    fun deleteTrafficNote(eventNote: EventNote) {
        viewModelScope.launch {
            try {
                trafficNoteRepository.deleteTrafficNote(eventNote)
            } catch (e: Exception) {
                // Handle exception
            }
        }
    }
}

class EventNoteViewModelFactory(private val application: Application) : ViewModelProvider.Factory {
    override fun <T : androidx.lifecycle.ViewModel> create(modelClass: Class<T>): T {
        if (modelClass.isAssignableFrom(EventNoteViewModel::class.java)) {
            @Suppress("UNCHECKED_CAST")
            return EventNoteViewModel(application) as T
        }
        throw IllegalArgumentException("Unknown ViewModel class")
    }
}